package org.openkoala.koala.wizard;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.openkoala.koala.console.ConsoleFactory;
import org.openkoala.koala.utils.ProjectCustomUtils;
import org.openkoala.koala.widget.Project;
import org.openkoala.koala.wizard.wizardpage.InfoSummaryWizardPage;
import org.openkoala.koala.wizard.wizardpage.MavenInfoWizardPage;
import org.openkoala.koala.wizard.wizardpage.ModulesInfoWizardPage;
import org.openkoala.koala.wizard.wizardpage.NameLocationWizardPage;
import org.openkoala.koala.wizard.wizardpage.ModuleConfigWizardPage;

/**
 * 恒拓开源平台的Wizard
 * 
 * @author lingen.liu
 * 
 */
public class KoalaWizard extends Wizard implements INewWizard {

	private IWorkbench workbench;
	private IStructuredSelection selection;
	
	//要被新建的项目
	private Project project = ProjectCustomUtils.customCommonProject();

	private NameLocationWizardPage nameLocationWizardPage;
	private MavenInfoWizardPage mavenInfoWizardPage;
	private ModulesInfoWizardPage modulesInfoWizardPage;
	private ModuleConfigWizardPage authorityWizardPage;
	private InfoSummaryWizardPage infoSummaryWizardPage;

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		ConsoleFactory.showConsole();
		this.workbench = workbench;
		this.selection = selection;
		setWindowTitle("New Project By Koala");
	}
	
	@Override
	public void createPageControls(Composite pageContainer) {
	    //super.createPageControls(pageContainer);
	}

	@Override
	public void addPages() {
		nameLocationWizardPage = new NameLocationWizardPage(workbench, selection, project);
		nameLocationWizardPage.setTitle("New Project");
		nameLocationWizardPage.setDescription("New project by koala");

		mavenInfoWizardPage = new MavenInfoWizardPage(project);
		modulesInfoWizardPage = new ModulesInfoWizardPage(workbench, project);
		authorityWizardPage = new ModuleConfigWizardPage(project);
		infoSummaryWizardPage = new InfoSummaryWizardPage(project);

		addPage(nameLocationWizardPage);
		addPage(mavenInfoWizardPage);
		addPage(modulesInfoWizardPage);
		addPage(authorityWizardPage);
		addPage(infoSummaryWizardPage);
	}

	@Override
	public boolean performFinish() {
		try {
			getContainer().run(true, true,
					new CreateImportProject(Display.getCurrent(), getShell(), project, CreateImportProject.CUSTOM_PROJECT));
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}

		return true;
	}

	@Override
	public boolean needsProgressMonitor() {
		return true;
	}

}
